{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#  决策树",
   "id": "c6ecf8b357c84630"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.276628Z",
     "start_time": "2025-02-15T12:51:42.273488Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "id": "7662ba1c0b131213",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.290040Z",
     "start_time": "2025-02-15T12:51:42.285762Z"
    }
   },
   "cell_type": "code",
   "source": "np.log2(1/32)",
   "id": "fa68566455337294",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-5.0)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.343413Z",
     "start_time": "2025-02-15T12:51:42.338547Z"
    }
   },
   "cell_type": "code",
   "source": "1 / 2 * np.log2(1 /2) + 1 / 2 * np.log2(1 /2)",
   "id": "22741338e5f8b658",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-1.0)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.349852Z",
     "start_time": "2025-02-15T12:51:42.345425Z"
    }
   },
   "cell_type": "code",
   "source": "1 / 3 * np.log2(1 / 3) + 2 / 3 * np.log2(2 / 3)",
   "id": "45f7ea5938c2e01b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-0.9182958340544896)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.369473Z",
     "start_time": "2025-02-15T12:51:42.357765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "决策树对泰坦尼克号进行预测生死\n",
    ":return: None\n",
    "\"\"\"\n",
    "# 获取数据\n",
    "titan = pd.read_csv(\"../data/titanic.txt\")\n",
    "titan.info()"
   ],
   "id": "a98eba7f5752cff4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 11 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   row.names  1313 non-null   int64  \n",
      " 1   pclass     1313 non-null   object \n",
      " 2   survived   1313 non-null   int64  \n",
      " 3   name       1313 non-null   object \n",
      " 4   age        633 non-null    float64\n",
      " 5   embarked   821 non-null    object \n",
      " 6   home.dest  754 non-null    object \n",
      " 7   room       77 non-null     object \n",
      " 8   ticket     69 non-null     object \n",
      " 9   boat       347 non-null    object \n",
      " 10  sex        1313 non-null   object \n",
      "dtypes: float64(1), int64(2), object(8)\n",
      "memory usage: 113.0+ KB\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.380655Z",
     "start_time": "2025-02-15T12:51:42.370480Z"
    }
   },
   "cell_type": "code",
   "source": "titan",
   "id": "69e86c2c59b8a41c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      row.names pclass  survived  \\\n",
       "0             1    1st         1   \n",
       "1             2    1st         0   \n",
       "2             3    1st         0   \n",
       "3             4    1st         0   \n",
       "4             5    1st         1   \n",
       "...         ...    ...       ...   \n",
       "1308       1309    3rd         0   \n",
       "1309       1310    3rd         0   \n",
       "1310       1311    3rd         0   \n",
       "1311       1312    3rd         0   \n",
       "1312       1313    3rd         0   \n",
       "\n",
       "                                                 name      age     embarked  \\\n",
       "0                        Allen, Miss Elisabeth Walton  29.0000  Southampton   \n",
       "1                         Allison, Miss Helen Loraine   2.0000  Southampton   \n",
       "2                 Allison, Mr Hudson Joshua Creighton  30.0000  Southampton   \n",
       "3     Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)  25.0000  Southampton   \n",
       "4                       Allison, Master Hudson Trevor   0.9167  Southampton   \n",
       "...                                               ...      ...          ...   \n",
       "1308                               Zakarian, Mr Artun      NaN          NaN   \n",
       "1309                           Zakarian, Mr Maprieder      NaN          NaN   \n",
       "1310                                  Zenn, Mr Philip      NaN          NaN   \n",
       "1311                                    Zievens, Rene      NaN          NaN   \n",
       "1312                                   Zimmerman, Leo      NaN          NaN   \n",
       "\n",
       "                            home.dest room      ticket   boat     sex  \n",
       "0                        St Louis, MO  B-5  24160 L221      2  female  \n",
       "1     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "2     Montreal, PQ / Chesterville, ON  C26         NaN  (135)    male  \n",
       "3     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "4     Montreal, PQ / Chesterville, ON  C22         NaN     11    male  \n",
       "...                               ...  ...         ...    ...     ...  \n",
       "1308                              NaN  NaN         NaN    NaN    male  \n",
       "1309                              NaN  NaN         NaN    NaN    male  \n",
       "1310                              NaN  NaN         NaN    NaN    male  \n",
       "1311                              NaN  NaN         NaN    NaN  female  \n",
       "1312                              NaN  NaN         NaN    NaN    male  \n",
       "\n",
       "[1313 rows x 11 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row.names</th>\n",
       "      <th>pclass</th>\n",
       "      <th>survived</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>embarked</th>\n",
       "      <th>home.dest</th>\n",
       "      <th>room</th>\n",
       "      <th>ticket</th>\n",
       "      <th>boat</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1st</td>\n",
       "      <td>1</td>\n",
       "      <td>Allen, Miss Elisabeth Walton</td>\n",
       "      <td>29.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>St Louis, MO</td>\n",
       "      <td>B-5</td>\n",
       "      <td>24160 L221</td>\n",
       "      <td>2</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Miss Helen Loraine</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Mr Hudson Joshua Creighton</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>(135)</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)</td>\n",
       "      <td>25.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1st</td>\n",
       "      <td>1</td>\n",
       "      <td>Allison, Master Hudson Trevor</td>\n",
       "      <td>0.9167</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1308</th>\n",
       "      <td>1309</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zakarian, Mr Artun</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1309</th>\n",
       "      <td>1310</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zakarian, Mr Maprieder</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1310</th>\n",
       "      <td>1311</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zenn, Mr Philip</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1311</th>\n",
       "      <td>1312</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zievens, Rene</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1312</th>\n",
       "      <td>1313</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zimmerman, Leo</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1313 rows × 11 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.406290Z",
     "start_time": "2025-02-15T12:51:42.398848Z"
    }
   },
   "cell_type": "code",
   "source": "titan.describe",
   "id": "60925fc2cc5d10b1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method NDFrame.describe of       row.names pclass  survived  \\\n",
       "0             1    1st         1   \n",
       "1             2    1st         0   \n",
       "2             3    1st         0   \n",
       "3             4    1st         0   \n",
       "4             5    1st         1   \n",
       "...         ...    ...       ...   \n",
       "1308       1309    3rd         0   \n",
       "1309       1310    3rd         0   \n",
       "1310       1311    3rd         0   \n",
       "1311       1312    3rd         0   \n",
       "1312       1313    3rd         0   \n",
       "\n",
       "                                                 name      age     embarked  \\\n",
       "0                        Allen, Miss Elisabeth Walton  29.0000  Southampton   \n",
       "1                         Allison, Miss Helen Loraine   2.0000  Southampton   \n",
       "2                 Allison, Mr Hudson Joshua Creighton  30.0000  Southampton   \n",
       "3     Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)  25.0000  Southampton   \n",
       "4                       Allison, Master Hudson Trevor   0.9167  Southampton   \n",
       "...                                               ...      ...          ...   \n",
       "1308                               Zakarian, Mr Artun      NaN          NaN   \n",
       "1309                           Zakarian, Mr Maprieder      NaN          NaN   \n",
       "1310                                  Zenn, Mr Philip      NaN          NaN   \n",
       "1311                                    Zievens, Rene      NaN          NaN   \n",
       "1312                                   Zimmerman, Leo      NaN          NaN   \n",
       "\n",
       "                            home.dest room      ticket   boat     sex  \n",
       "0                        St Louis, MO  B-5  24160 L221      2  female  \n",
       "1     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "2     Montreal, PQ / Chesterville, ON  C26         NaN  (135)    male  \n",
       "3     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "4     Montreal, PQ / Chesterville, ON  C22         NaN     11    male  \n",
       "...                               ...  ...         ...    ...     ...  \n",
       "1308                              NaN  NaN         NaN    NaN    male  \n",
       "1309                              NaN  NaN         NaN    NaN    male  \n",
       "1310                              NaN  NaN         NaN    NaN    male  \n",
       "1311                              NaN  NaN         NaN    NaN  female  \n",
       "1312                              NaN  NaN         NaN    NaN    male  \n",
       "\n",
       "[1313 rows x 11 columns]>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.426662Z",
     "start_time": "2025-02-15T12:51:42.412710Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理数据，找出特征值和目标值\n",
    "x = titan[['pclass', 'age', 'sex']]\n",
    "\n",
    "y = titan['survived']\n",
    "print(x.info())  # 用来判断是否有空值\n",
    "x.describe(include='all')"
   ],
   "id": "eeb9465b7a10a04",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   pclass  1313 non-null   object \n",
      " 1   age     633 non-null    float64\n",
      " 2   sex     1313 non-null   object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 30.9+ KB\n",
      "None\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "       pclass         age   sex\n",
       "count    1313  633.000000  1313\n",
       "unique      3         NaN     2\n",
       "top       3rd         NaN  male\n",
       "freq      711         NaN   850\n",
       "mean      NaN   31.194181   NaN\n",
       "std       NaN   14.747525   NaN\n",
       "min       NaN    0.166700   NaN\n",
       "25%       NaN   21.000000   NaN\n",
       "50%       NaN   30.000000   NaN\n",
       "75%       NaN   41.000000   NaN\n",
       "max       NaN   71.000000   NaN"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pclass</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1313</td>\n",
       "      <td>633.000000</td>\n",
       "      <td>1313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>3rd</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>711</td>\n",
       "      <td>NaN</td>\n",
       "      <td>850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>NaN</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>NaN</td>\n",
       "      <td>14.747525</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.166700</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>21.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>NaN</td>\n",
       "      <td>71.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.435701Z",
     "start_time": "2025-02-15T12:51:42.427671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一定要进行缺失值处理,填为均值\n",
    "mean=x['age'].mean()\n",
    "print(mean)\n",
    "x.loc[:,'age']=x.loc[:,'age'].fillna(mean)\n",
    "\n",
    "\n",
    "# 分割数据集到训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "print(x_train.head())"
   ],
   "id": "75f1f900f1cddf76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31.19418104265403\n",
      "    pclass        age     sex\n",
      "598    2nd  30.000000    male\n",
      "246    1st  62.000000    male\n",
      "905    3rd  31.194181  female\n",
      "300    1st  31.194181  female\n",
      "509    2nd  64.000000    male\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.454376Z",
     "start_time": "2025-02-15T12:51:42.449210Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#性别是女性的数量\n",
    "x_train[x_train['sex'] == 'female'].count()"
   ],
   "id": "743e454665852f2e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pclass    341\n",
       "age       341\n",
       "sex       341\n",
       "dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.484963Z",
     "start_time": "2025-02-15T12:51:42.478488Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#女性中存活的情况对比\n",
    "z=x_train.copy() #z是为了把特征和目标存储到一起\n",
    "z['survived'] = y_train #把目标值存储到z中\n",
    "z[z['sex'] == 'female']['survived'].value_counts()  #男性中存活的情况"
   ],
   "id": "f0da308f73a9ec40",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "1    230\n",
       "0    111\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.617175Z",
     "start_time": "2025-02-15T12:51:42.611701Z"
    }
   },
   "cell_type": "code",
   "source": "z[z['sex'] == 'male']['survived'].value_counts() ",
   "id": "7ce0a3247324f9be",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "0    539\n",
       "1    104\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.682145Z",
     "start_time": "2025-02-15T12:51:42.676522Z"
    }
   },
   "cell_type": "code",
   "source": "y_train.value_counts() #没存活的是650，存活的是334",
   "id": "8c4b31dda5e6b03e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "0    650\n",
       "1    334\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.782031Z",
     "start_time": "2025-02-15T12:51:42.770541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x_train.to_dict(orient=\"records\") #把df变为字典，样本变为一个一个的字典，字典中列名变为键\n",
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取,to_dict可以把df变为字典，records代表列名变为键\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "print(type(x_train))\n",
    "print(dict.get_feature_names_out())\n",
    "print('-' * 50)\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "print(x_train)"
   ],
   "id": "9c20085cea09a6ed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "['age' 'pclass=1st' 'pclass=2nd' 'pclass=3rd' 'sex=female' 'sex=male']\n",
      "--------------------------------------------------\n",
      "[[30.          0.          1.          0.          0.          1.        ]\n",
      " [62.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          1.          0.        ]\n",
      " ...\n",
      " [34.          0.          1.          0.          0.          1.        ]\n",
      " [46.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          0.          1.        ]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T12:51:42.808394Z",
     "start_time": "2025-02-15T12:51:42.783413Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 用决策树进行预测，修改max_depth试试,修改criterion为entropy\n",
    "#树过于复杂，就会产生过拟合\n",
    "dec = DecisionTreeClassifier()\n",
    "\n",
    "#训练\n",
    "dec.fit(x_train, y_train)\n",
    "\n",
    "# 预测准确率\n",
    "print(\"预测的准确率：\", dec.score(x_test, y_test))\n",
    "\n",
    "# 导出决策树的结构\n",
    "export_graphviz(dec, out_file=\"tree.dot\",\n",
    "                feature_names=['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', 'female', 'male'])\n"
   ],
   "id": "d41507b0b230912b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的准确率： 0.8085106382978723\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T13:08:34.715916Z",
     "start_time": "2025-02-15T13:08:34.711125Z"
    }
   },
   "cell_type": "code",
   "source": "np.log2(1/2)*1/2*2",
   "id": "2c43d504cbe5457f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-1.0)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-15T13:30:23.545200Z",
     "start_time": "2025-02-15T13:30:23.540198Z"
    }
   },
   "cell_type": "code",
   "source": "np.log2(1/3)*1/3 + np.log2(2/3)*2/3",
   "id": "a1ae74992850a8e4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-0.9182958340544896)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "a863650ad932be5c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "\n",
   "id": "6dfc6c71141426f8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
